package com.wangijun.yuan.web.api;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wangijun.yuan.web.base.CommonPage;
import com.wangijun.yuan.web.base.CommonResult;
import com.wangijun.yuan.core.domain.UserSetting;
import com.wangijun.yuan.core.model.BoolParam;
import com.wangijun.yuan.core.model.em.NoticeTypeEnum;
import com.wangijun.yuan.web.model.dto.NoticeQryParam;
import com.wangijun.yuan.web.model.vo.NoticeCountVO;
import com.wangijun.yuan.web.model.vo.NoticeInfoVO;
import com.wangijun.yuan.web.model.vo.SelectVO;
import com.wangijun.yuan.web.service.NoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Tag(name = "A-通知相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/notice")
public class NoticeController {
    private final NoticeService noticeService;

    @Operation(summary = "关注用户发布更新文章通知")
    @PostMapping("/art")
    CommonResult<CommonPage<NoticeInfoVO>> noticeArt(@RequestBody NoticeQryParam param) {
        return CommonResult.data(noticeService.noticeArt(param));
    }

    @Operation(summary = "关注用户发布更新文章通知")
    @PostMapping("/people")
    CommonResult<CommonPage<NoticeInfoVO>> noticePeople(@RequestBody NoticeQryParam param) {
        return CommonResult.data(noticeService.noticePeople(param));
    }

    @Operation(summary = "关注用户发布更新文章通知")
    @PostMapping("/favorite")
    CommonResult<CommonPage<NoticeInfoVO>> noticeFavorite(@RequestBody NoticeQryParam param) {
        return CommonResult.data(noticeService.noticeFavorite(param));
    }

    @Operation(summary = "通知红点标识")
    @PostMapping("/tag")
    CommonResult<NoticeCountVO> noticeTag() {
        return CommonResult.data(noticeService.noticeTag());
    }


    @Schema(description = "已通知接口")
    public record FlagParam(
            @Schema(description = "通知类型")
            NoticeTab tab
    ) {

    }

    public enum NoticeTab {
        art,
        people,
        favorite,

    }

    @Operation(summary = "更新通知标识")
    @PostMapping("/flag")
    CommonResult<Boolean> noticeFlag(@RequestBody FlagParam param) {
        return CommonResult.data(noticeService.noticeFlag(param));
    }


    @SaCheckLogin
    @Operation(summary = "隐藏红点")
    @PostMapping("/hide-red")
    CommonResult<Boolean> noticeHideRed(@RequestBody BoolParam param) {
        Db.lambdaUpdate(UserSetting.class)
                .eq(UserSetting::getUserId, StpUtil.getLoginIdAsLong())
                .set(UserSetting::getHideRed, param.getBool())
                .update();
        return CommonResult.ok();
    }

    @Operation(summary = "查看全部通知")
    @PostMapping("/list")
    CommonResult<CommonPage<NoticeInfoVO>> noticeList(@RequestBody NoticeQryParam param) {
        return CommonResult.data(noticeService.noticeList(param));
    }

    @Operation(description = "查询参数")
    @GetMapping("/init")
    CommonResult<List<SelectVO>> noticeInit() {
        List<SelectVO> list = new ArrayList<>(
                List.of(new SelectVO("全部类别", -1))
        );
        for (NoticeTypeEnum value : NoticeTypeEnum.values()) {
            list.add(new SelectVO(value.getMessage(), value.getCode()));
        }
        return CommonResult.data(list);
    }

}
